package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.RoleAuthorityDAO;
import cn.edu.scau.cmi.hombio.dao.RoleDAO;
import cn.edu.scau.cmi.hombio.dao.RoleEmployeeDAO;

import cn.edu.scau.cmi.hombio.domain.Role;
import cn.edu.scau.cmi.hombio.domain.RoleAuthority;
import cn.edu.scau.cmi.hombio.domain.RoleEmployee;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Role entities
 * 
 */

@Service("RoleService")

@Transactional
public class RoleServiceImpl implements RoleService {

	/**
	 * DAO injected by Spring that manages RoleAuthority entities
	 * 
	 */
	@Autowired
	private RoleAuthorityDAO roleAuthorityDAO;

	/**
	 * DAO injected by Spring that manages Role entities
	 * 
	 */
	@Autowired
	private RoleDAO roleDAO;

	/**
	 * DAO injected by Spring that manages RoleEmployee entities
	 * 
	 */
	@Autowired
	private RoleEmployeeDAO roleEmployeeDAO;

	/**
	 * Instantiates a new RoleServiceImpl.
	 *
	 */
	public RoleServiceImpl() {
	}

	/**
	 * Delete an existing Role entity
	 * 
	 */
	@Transactional
	public void deleteRole(Role role) {
		roleDAO.remove(role);
		roleDAO.flush();
	}

	/**
	 * Return all Role entity
	 * 
	 */
	@Transactional
	public List<Role> findAllRoles(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Role>(roleDAO.findAllRoles(startResult, maxRows));
	}

	/**
	 * Return a count of all Role entity
	 * 
	 */
	@Transactional
	public Integer countRoles() {
		return ((Long) roleDAO.createQuerySingleResult("select count(o) from Role o").getSingleResult()).intValue();
	}

	/**
	 */
	@Transactional
	public Role findRoleByPrimaryKey(Integer id) {
		return roleDAO.findRoleByPrimaryKey(id);
	}

	/**
	 * Delete an existing RoleAuthority entity
	 * 
	 */
	@Transactional
	public Role deleteRoleRoleAuthorities(Integer role_id, Integer related_roleauthorities_id) {
		RoleAuthority related_roleauthorities = roleAuthorityDAO.findRoleAuthorityByPrimaryKey(related_roleauthorities_id, -1, -1);

		Role role = roleDAO.findRoleByPrimaryKey(role_id, -1, -1);

		related_roleauthorities.setRole(null);
		role.getRoleAuthorities().remove(related_roleauthorities);

		roleAuthorityDAO.remove(related_roleauthorities);
		roleAuthorityDAO.flush();

		return role;
	}

	/**
	 * Save an existing RoleAuthority entity
	 * 
	 */
	@Transactional
	public Role saveRoleRoleAuthorities(Integer id, RoleAuthority related_roleauthorities) {
		Role role = roleDAO.findRoleByPrimaryKey(id, -1, -1);
		RoleAuthority existingroleAuthorities = roleAuthorityDAO.findRoleAuthorityByPrimaryKey(related_roleauthorities.getId());

		// copy into the existing record to preserve existing relationships
		if (existingroleAuthorities != null) {
			existingroleAuthorities.setId(related_roleauthorities.getId());
			existingroleAuthorities.setAdd(related_roleauthorities.getAdd());
			existingroleAuthorities.setDelete(related_roleauthorities.getDelete());
			existingroleAuthorities.setCheck(related_roleauthorities.getCheck());
			existingroleAuthorities.setEdit(related_roleauthorities.getEdit());
			existingroleAuthorities.setMeno(related_roleauthorities.getMeno());
			related_roleauthorities = existingroleAuthorities;
		}

		related_roleauthorities.setRole(role);
		role.getRoleAuthorities().add(related_roleauthorities);
		related_roleauthorities = roleAuthorityDAO.store(related_roleauthorities);
		roleAuthorityDAO.flush();

		role = roleDAO.store(role);
		roleDAO.flush();

		return role;
	}

	/**
	 * Delete an existing RoleEmployee entity
	 * 
	 */
	@Transactional
	public Role deleteRoleRoleEmployees(Integer role_id, Integer related_roleemployees_id) {
		RoleEmployee related_roleemployees = roleEmployeeDAO.findRoleEmployeeByPrimaryKey(related_roleemployees_id, -1, -1);

		Role role = roleDAO.findRoleByPrimaryKey(role_id, -1, -1);

		related_roleemployees.setRole(null);
		role.getRoleEmployees().remove(related_roleemployees);

		roleEmployeeDAO.remove(related_roleemployees);
		roleEmployeeDAO.flush();

		return role;
	}

	/**
	 * Save an existing RoleEmployee entity
	 * 
	 */
	@Transactional
	public Role saveRoleRoleEmployees(Integer id, RoleEmployee related_roleemployees) {
		Role role = roleDAO.findRoleByPrimaryKey(id, -1, -1);
		RoleEmployee existingroleEmployees = roleEmployeeDAO.findRoleEmployeeByPrimaryKey(related_roleemployees.getId());

		// copy into the existing record to preserve existing relationships
		if (existingroleEmployees != null) {
			existingroleEmployees.setId(related_roleemployees.getId());
			existingroleEmployees.setMemo(related_roleemployees.getMemo());
			related_roleemployees = existingroleEmployees;
		}

		related_roleemployees.setRole(role);
		role.getRoleEmployees().add(related_roleemployees);
		related_roleemployees = roleEmployeeDAO.store(related_roleemployees);
		roleEmployeeDAO.flush();

		role = roleDAO.store(role);
		roleDAO.flush();

		return role;
	}

	/**
	 * Load an existing Role entity
	 * 
	 */
	@Transactional
	public Set<Role> loadRoles() {
		return roleDAO.findAllRoles();
	}

	/**
	 * Save an existing Role entity
	 * 
	 */
	@Transactional
	public void saveRole(Role role) {
		Role existingRole = roleDAO.findRoleByPrimaryKey(role.getId());

		if (existingRole != null) {
			if (existingRole != role) {
				existingRole.setId(role.getId());
				existingRole.setName(role.getName());
				existingRole.setMemo(role.getMemo());
			}
			role = roleDAO.store(existingRole);
		} else {
			role = roleDAO.store(role);
		}
		roleDAO.flush();
	}
}
